package upload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.GoodsService;
import service.TgoodsService;
import sun.misc.BASE64Decoder;

public class UploadImgServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public UploadImgServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String pic = request.getParameter("pic");
		String pic1 = request.getParameter("pic1");
		String pic2 = request.getParameter("pic2");
		String pic3 = request.getParameter("pic3");

		String path = request.getSession().getServletContext().getRealPath("/")
				+ "images/";
		String params[] = request.getParameter("params").split(",");
		String action = params[0];
		String type = params[1];
		String imageOldName = params[2];
		String random = params[3];

		// String random = (100 + new Random().nextInt(899)) + "";
		if ("goods".equals(type)) {
			path += "goods/";
			// imgName = "seller" + type[1] + "g" + type[2] + "_" + random;
		} else if ("tgoods".equals(type)) {
			path += "tgoods/";
			// imgName = "seller" + type[1] + "tg" + type[2] + "_" + random;
		}

		String temp[] = imageOldName.split("_");
		String imgName = temp[0] + "_" + random;

		// 删除以前的图片
		if ("change".equals(action)) {
			deleteOldFile(path, imageOldName);
		}

		// 原图
		if (!("".equals(pic) || pic == null)) {
			File file = new File(path + imgName + "_src.png");
			FileOutputStream fout = null;
			fout = new FileOutputStream(file);
			fout.write(new BASE64Decoder().decodeBuffer(pic));
			fout.close();
		}
		// 图1
		File file1 = new File(path + imgName + "_1.png");
		FileOutputStream fout1 = null;
		fout1 = new FileOutputStream(file1);
		fout1.write(new BASE64Decoder().decodeBuffer(pic1));
		fout1.close();

		// 图2
		File file2 = new File(path + imgName + "_2.png");
		FileOutputStream fout2 = null;
		fout2 = new FileOutputStream(file2);
		fout2.write(new BASE64Decoder().decodeBuffer(pic2));
		fout2.close();

		// 图3
		File file3 = new File(path + imgName + "_3.png");
		FileOutputStream fout3 = null;
		fout3 = new FileOutputStream(file3);
		fout3.write(new BASE64Decoder().decodeBuffer(pic3));
		fout3.close();

		if ("change".equals(action)) {
			try {
				if ("tgoods".equals(type)) {
					TgoodsService.changeImageUrl("images/tgoods/"
							+ imageOldName, "images/tgoods/" + imgName);
				} else {
					GoodsService.changeGoodsImg("images/goods/" + imageOldName,
							"images/goods/" + imgName);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		out.println("{\"status\":1}");
		out.flush();
		out.close();
	}

	private void deleteOldFile(String path, String fileName) {
		String temp[] = fileName.split("_");
		String f1 = fileName;
		String f2 = temp[0] + "_" + temp[1] + "_2.png";
		String f3 = temp[0] + "_" + temp[1] + "_3.png";
		System.out.println(f1 + "-" + f2 + "-" + f3);
		File oldFile1 = new File(path + f1);
		if (oldFile1.exists()) {
			oldFile1.delete();
		}
		File oldFile2 = new File(path + f2);
		if (oldFile2.exists()) {
			oldFile2.delete();
		}
		File oldFile3 = new File(path + f3);
		if (oldFile3.exists()) {
			oldFile3.delete();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
